/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ai.loa.framework.view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

import ai.loa.ai.Stato;
import ai.loa.framework.LOABoard;
import ai.loa.framework.LOABoardListener;
import ai.loa.framework.LOAEvent;
import ai.loa.framework.controller.LOAController;
import ai.loa.framework.controller.LOAGameObserver;
import ai.loa.framework.controller.LocalLOAController;

/**
 * 
 * @author root
 */
@SuppressWarnings("serial")
public class SwingLOAView extends JComponent implements LOABoardListener,
		LOAGameObserver {

	private LOAImageFactory factory;
	private Dimension size;
	private Stato board;
	public LOALabel[][] labels;
	private LOAController controller;

	public SwingLOAView(LOAImageFactory f, Stato stato, LOAController controller) {
		super();
		factory = f;
		BufferedImage img = factory.getImage(LOAImageFactory.IMG_BOARD);
		size = new Dimension(img.getWidth(), img.getHeight());
		board = stato;
		if (controller == null)
			this.controller = new LocalLOAController(LOABoard.PLAYER_1);
		else
			this.controller = controller;
		board.addBoardListener(this);
		setLayout(null);
		labels = new LOALabel[LOABoard.BOARD_SIZE][LOABoard.BOARD_SIZE];
		prepareLabels();
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		drawBoard(g);
	}

	@Override
	public Dimension getPreferredSize() {
		return size;
	}

	@Override
	public Dimension getMinimumSize() {
		return size;
	}

	@Override
	public Dimension getMaximumSize() {
		return size;
	}

	public void boardChanged(LOAEvent event) {
		prepareLabels();
	}

	private void prepareLabels() {

		int basex = factory.getBaseX();
		int basey = factory.getBaseY();
		int width = factory.getCellWidth();
		int height = factory.getCellHeight();

		for (int i = 0; i < LOABoard.BOARD_SIZE; i++) {
			for (int j = 0; j < LOABoard.BOARD_SIZE; j++) {
				if (labels[i][j] == null) {
					labels[i][j] = new LOALabel(board, factory, controller, i,
							j, this);
					labels[i][j].setLocation(basex + j * width, basey + i
							* height);
					add(labels[i][j]);
				}
			}
		}
	}

	private void drawBoard(Graphics g) {
		g.drawImage(factory.getImage(LOAImageFactory.IMG_BOARD), 0, 0, this);
	}

	public void turnChanged(Stato stato) {
		System.out.println("Tocca a te!");
		Toolkit.getDefaultToolkit().beep();
	}

	public void highlightAsDestination(int i, int j, int sourceX, int sourceY) {
		labels[i][j].destination = true;
		labels[i][j].source[0] = sourceX;
		labels[i][j].source[1] = sourceY;
		for (int k = 0; k < LOABoard.BOARD_SIZE; k++) {
			for (int h = 0; h < LOABoard.BOARD_SIZE; h++) {
				labels[k][h].boardChanged(new LOAEvent(k, h));
			}
		}
	}

	public void updateAll() {
		for (int k = 0; k < LOABoard.BOARD_SIZE; k++) {
			for (int h = 0; h < LOABoard.BOARD_SIZE; h++) {
				labels[k][h].boardChanged(new LOAEvent(k, h));
			}
		}
	}

	public void unhighlightAll() {
		for (int k = 0; k < LOABoard.BOARD_SIZE; k++) {
			for (int h = 0; h < LOABoard.BOARD_SIZE; h++) {
				labels[k][h].destination = false;
				labels[k][h].source[0] = -1;
				labels[k][h].source[1] = -1;
				labels[k][h].boardChanged(new LOAEvent(k, h));
			}
		}

	}
}
